/*********************************************************************************
 * Copyright(C),2015, xes peiyou
 * Author:      yaobyron@sina.com
 * Version:     2.0.0
 * Date:        20150615
 * Description:
        MFRC522模块驱动
 * History:
        Date            Author                  Modification
        20150601        yaobyron@sina.com       add comments
        20150615        yaobyron@sina.com       support buzzer
**********************************************************************************/

#ifndef __SPI_RC522_H__
#define __SPI_RC522_H__

#include <linux/ioctl.h>

typedef struct {
    int     times;
    int     timeout;    // milliseconds
} rc522_buzzer_t;

#define RC522_BLKDATA_SIZE              16
#define RC522_MAXRXLEN                  18
#define RC522_DATA_MAXSIZE              64

/////////////////////////////////////////////////////////////////////
//MF522命令字
/////////////////////////////////////////////////////////////////////
#define RC522_PCD_IDLE                  0x00        //取消当前命令
#define RC522_PCD_AUTHENT               0x0E        //验证密钥
#define RC522_PCD_RECEIVE               0x08        //接收数据
#define RC522_PCD_TRANSMIT              0x04        //发送数据
#define RC522_PCD_TRANSCEIVE            0x0C        //发送并接收数据
#define RC522_PCD_RESETPHASE            0x0F        //复位
#define RC522_PCD_CALCCRC               0x03        //CRC计算

/////////////////////////////////////////////////////////////////////
//Mifare_One卡片命令字
/////////////////////////////////////////////////////////////////////
#define RC522_PICC_REQIDL               0x26        //寻天线区内未进入休眠状态
#define RC522_PICC_REQALL               0x52        //寻天线区内全部卡
#define RC522_PICC_ANTICOLL1            0x93        //防冲撞
#define RC522_PICC_ANTICOLL2            0x95        //防冲撞
#define RC522_PICC_AUTHENT1A            0x60        //验证A密钥
#define RC522_PICC_AUTHENT1B            0x61        //验证B密钥
#define RC522_PICC_READ                 0x30        //读块
#define RC522_PICC_WRITE                0xA0        //写块
#define RC522_PICC_DECREMENT            0xC0        //扣款
#define RC522_PICC_INCREMENT            0xC1        //充值
#define RC522_PICC_RESTORE              0xC2        //调块数据到缓冲区
#define RC522_PICC_TRANSFER             0xB0        //保存缓冲区中数据
#define RC522_PICC_HALT                 0x50        //休眠

/////////////////////////////////////////////////////////////////////
//MF522 FIFO长度定义
/////////////////////////////////////////////////////////////////////
#define RC522_DEF_FIFO_LENGTH           64          //FIFO size=64byte

/////////////////////////////////////////////////////////////////////
//MF522寄存器定义
/////////////////////////////////////////////////////////////////////
// PAGE 0
#define RC522_RFU00                     0x00
#define RC522_CommandReg                0x01
#define RC522_ComIEnReg                 0x02
#define RC522_DivlEnReg                 0x03
#define RC522_ComIrqReg                 0x04
#define RC522_DivIrqReg                 0x05
#define RC522_ErrorReg                  0x06    
#define RC522_Status1Reg                0x07    
#define RC522_Status2Reg                0x08    
#define RC522_FIFODataReg               0x09
#define RC522_FIFOLevelReg              0x0A
#define RC522_WaterLevelReg             0x0B
#define RC522_ControlReg                0x0C
#define RC522_BitFramingReg             0x0D
#define RC522_CollReg                   0x0E
#define RC522_RFU0F                     0x0F
// PAGE 1     
#define RC522_RFU10                     0x10
#define RC522_ModeReg                   0x11
#define RC522_TxModeReg                 0x12
#define RC522_RxModeReg                 0x13
#define RC522_TxControlReg              0x14
#define RC522_TxAutoReg                 0x15
#define RC522_TxSelReg                  0x16
#define RC522_RxSelReg                  0x17
#define RC522_RxThresholdReg            0x18
#define RC522_DemodReg                  0x19
#define RC522_RFU1A                     0x1A
#define RC522_RFU1B                     0x1B
#define RC522_MifareReg                 0x1C
#define RC522_RFU1D                     0x1D
#define RC522_RFU1E                     0x1E
#define RC522_SerialSpeedReg            0x1F
// PAGE 2    
#define RC522_RFU20                     0x20  
#define RC522_CRCResultRegM             0x21
#define RC522_CRCResultRegL             0x22
#define RC522_RFU23                     0x23
#define RC522_ModWidthReg               0x24
#define RC522_RFU25                     0x25
#define RC522_RFCfgReg                  0x26
#define RC522_GsNReg                    0x27
#define RC522_CWGsCfgReg                0x28
#define RC522_ModGsCfgReg               0x29
#define RC522_TModeReg                  0x2A
#define RC522_TPrescalerReg             0x2B
#define RC522_TReloadRegH               0x2C
#define RC522_TReloadRegL               0x2D
#define RC522_TCounterValueRegH         0x2E
#define RC522_TCounterValueRegL         0x2F
// PAGE 3      
#define RC522_RFU30                     0x30
#define RC522_TestSel1Reg               0x31
#define RC522_TestSel2Reg               0x32
#define RC522_TestPinEnReg              0x33
#define RC522_TestPinValueReg           0x34
#define RC522_TestBusReg                0x35
#define RC522_AutoTestReg               0x36
#define RC522_VersionReg                0x37
#define RC522_AnalogTestReg             0x38
#define RC522_TestDAC1Reg               0x39  
#define RC522_TestDAC2Reg               0x3A   
#define RC522_TestADCReg                0x3B   
#define RC522_RFU3C                     0x3C   
#define RC522_RFU3D                     0x3D   
#define RC522_RFU3E                     0x3E   
#define RC522_RFU3F                     0x3F

#define RC522_REQ_ALL                   0x52
#define RC522_KEYA                      0x60

/////////////////////////////////////////////////////////////////////
//和MF522通讯时返回的错误代码
/////////////////////////////////////////////////////////////////////
#define RC522_MI_OK                     (char)0
#define RC522_MI_NOTAGERR               (char)(-1)
#define RC522_MI_ERR                    (char)(-2)

// ioctl command
#define RC522_IOC_MAGIC                 'R'

#define RC522_CMD_ISREADY               _IO(RC522_IOC_MAGIC, 1)
#define RC522_CMD_SELECTCARD            _IOR(RC522_IOC_MAGIC, 2, unsigned char[6])
#define RC522_CMD_CHANGEBLK             _IOW(RC522_IOC_MAGIC, 3, int)
#define RC522_CMD_RESETCARD             _IO(RC522_IOC_MAGIC, 4)
#define RC522_CMD_HALTCARD              _IO(RC522_IOC_MAGIC, 5)
#define RC522_CMD_POLL                  _IO(RC522_IOC_MAGIC, 6)
#define RC522_CMD_BUZZER                _IOW(RC522_IOC_MAGIC, 7, rc522_buzzer_t*)

#define RC522_IOC_MAXNR                 7

#endif

